#coding=utf-8
# !/usr/bin/python
# -*- coding: utf-8 -*-

from com import * 
import struct
from PySide6.QtGui import QPalette, QColor

class Bitcfg:
    def __init__(self, ui):
        self.ui = ui
        self.com = ui.com
        self.wl_wirte =[self.ui.lineEdit_193, self.ui.lineEdit_194,
                        self.ui.lineEdit_195, self.ui.lineEdit_196,
                        self.ui.lineEdit_197, self.ui.lineEdit_198,
                        self.ui.lineEdit_199, self.ui.lineEdit_200,
                        self.ui.lineEdit_201, self.ui.lineEdit_202]
        
        self.wl_read = [self.ui.lineEdit_205, self.ui.lineEdit_206,
                        self.ui.lineEdit_207, self.ui.lineEdit_208,
                        self.ui.lineEdit_209, self.ui.lineEdit_210,
                        self.ui.lineEdit_211, self.ui.lineEdit_212,
                        self.ui.lineEdit_213, self.ui.lineEdit_214]
                        
  
        self.val = [i for i in range( len(self.wl_wirte) )]
        self.readval = [i for i in range( len(self.wl_wirte) )]
        self.setupui()
        self.UISend_button1 = self.ui.pushButton_19
        self.UISend_button2 = self.ui.pushButton_20
        self.UISend_button3 = self.ui.pushButton_6
        self.UISend_button4 = self.ui.pushButton_7
        self.UISend_button5 = self.ui.pushButton_17
        
        self.UISend_button1.clicked.connect(self.ui_pushButton1_cb)
        self.UISend_button2.clicked.connect(self.ui_pushButton2_cb)
        self.UISend_button3.clicked.connect(self.load_default)
        self.UISend_button4.clicked.connect(self.wirte_default)
        self.UISend_button5.clicked.connect(self.load_readval)
          
    def setupui(self):
        for i in range( len(self.wl_wirte) ):
            self.wl_wirte[i].setInputMask("0-00-0-0-0")
        self.load_default()
        
    def load_ui_cb(self):
        for i in range( len(self.wl_wirte) ):
            n = self.val[i]
            node_str = "{:d}-{:0>2d}-{:d}-{:d}-{:d}".format(n&0x1, \
                (n>>1)&0x1F, (n>>7)&0x1, (n>>8)&0x1, (n>>9)&0x7)
            self.wl_wirte[i].setText(node_str)    

 
    def load_default(self):
        for i in range( len(self.wl_wirte) ):
            self.val[i] = self.ui.cfg.data['bitcfg'][i]
        self.load_ui_cb()
        
    def wirte_default(self):
        for i in range( len(self.wl_wirte) ):
            self.ui.cfg.data['bitcfg'][i] = self.val[i]
        self.ui.cfg.save()
   
    def load_readval(self):   
        for i in range( len(self.wl_wirte) ):
            self.val[i] = self.readval[i]
        self.load_ui_cb()
        
    def ui_pushButton1_cb(self):
        self.ui_cb_send_readmsg()

    def ui_pushButton2_cb(self):
        for i in range( len(self.wl_wirte) ):
            str = self.wl_wirte[i].text()
            n1, n2, n3 , n4, n5 = int(str[0:1]), int(str[2:4]), int(str[5:6]), int(str[7:8]), int(str[9:10])
            self.val[i] = n1 + (n2<<1) + (n3<<7) + (n4<<8) + (n5<<9)
            print("write i =", i, n1, n2, n3 , n4, n5);
        self.ui_cb_send_writemsg()
        

    def ui_cb_send_readmsg(self):
        if self.com.serdev.ser.isOpen():
            array = self.com.pack_frame_head(0x24, 0x64)
            self.com.pack_frame_tail(array)
            self.com.serdev.ser.write(array)
        else:
            pass  
            
    def ui_cb_send_writemsg(self):
        if self.com.serdev.ser.isOpen():
            array = self.com.pack_frame_head(0x24, 0x34)
            self.pack_wirtemsg(array)
            self.com.pack_frame_tail(array)
            self.com.serdev.ser.write(array)
        else:
            pass
            
    def pack_wirtemsg(self, array):
        for i in range( len(self.wl_wirte) ):
            array[7+2*i] = self.val[i] & 0xff
            array[8+2*i] = (self.val[i] >> 8)& 0xff 
            print("self.val", self.val[i])
 
    def decode(self, a):
        for i in range( len(self.wl_read) ):
            self.readval[i] = struct.unpack('<H', a[(7+2*i):(9+2*i)])[0]
            print("self.readval", self.readval[i])
            n = self.readval[i]
            node_str = "{:d}-{:0>2d}-{:d}-{:d}-{:d}".format(n&0x1, \
                (n>>1)&0x1F, (n>>7)&0x1, (n>>8)&0x1, (n>>9)&0x7)
            print("decode i =", i, node_str);    
            self.wl_read[i].setText(node_str)
        

     

        

     



           

